package org.itboys.core.service.mongo.front;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.goods.SundayGoodsDto;
import org.itboys.core.dto.goods.SundayGoodsSizeDto;
import org.itboys.core.dto.scene.SundaySceneDto;
import org.itboys.core.entity.mongo.SundayScene;
import org.itboys.core.service.mongo.web.SundayGoodsService;
import org.itboys.core.service.mongo.web.SundayGoodsSizeService;
import org.itboys.core.service.mongo.web.SundaySceneService;
import org.itboys.core.util.JNBYRequestUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作者：zzq
 * 时间：2018/10/22
 * 描述：场景
 */
@Service
public class SundayFrontSceneService extends BaseMongoService<SundayScene> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Autowired
    private SundayGoodsService goodsService;
    @Autowired
    private SundaySceneService sceneService;
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private SundayGoodsSizeService goodsSizeService;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayScene> getEntityClass() {
        return SundayScene.class;
    }

    public List<SundaySceneDto> select() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("status", SundayCommonConstants.YES);
        List<SundaySceneDto> scenes = sceneService.packageMultiDto(super.list(param));
        return scenes;
    }

    /**
     * 查询单个
     *
     * @param sceneId
     * @return
     * @throws IOException
     */
    public SundaySceneDto findOne(Long sceneId) {
        SundaySceneDto scene = sceneService.packageSingleDto(super.getById(sceneId));
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("sceneId", sceneId);
        List<SundayGoodsDto> goodss = goodsService.packageMultiDto(goodsService.list(param));
        List<Long> goodsIds = goodss.stream().map(e -> e.getId()).collect(Collectors.toList());
        param.clear();
        param.put("sundayGoodsId in", goodsIds);
        param.put("isDeleted", SundayCommonConstants.NO);
        List<SundayGoodsSizeDto> allSizeDtos = goodsSizeService.packageMultiDto(goodsSizeService.list(param));
        //组装尺码
        for (SundayGoodsDto goodsDto : goodss) {
            List<SundayGoodsSizeDto> sizeDtos = new ArrayList<>();
            for (SundayGoodsSizeDto sizeDto : allSizeDtos) {
                if (sizeDto.getSundayGoodsId().longValue() == goodsDto.getId()) {
                    sizeDtos.add(sizeDto);
                }
            }
            goodsDto.setSizeDtos(sizeDtos);
        }
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(sceneId, SundayScene.class.getSimpleName());
        scene.setRollImages(rollImages);
        scene.setGoodsDtos(goodss);
        return scene;

    }
}
